package unit.entity.basic;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import am.bll.entity.basic.Quantifier;
import am.exception.ArgumentNullException;
import am.exception.ArgumentOutOfRangeException;

public class QuantifierTester
{

    @Before
    public void setUp() throws Exception
    {
    }

    @After
    public void tearDown() throws Exception
    {
    }

    @SuppressWarnings("rawtypes")
    private void testConstructorException(int num, String unit, Class className)
    {
        try
        {
            Quantifier q = new Quantifier(num, unit);
            fail("q=" + q.toString() + "  但是q本应该抛出异常");
        }
        catch (Exception e)
        {
            assertTrue(className == e.getClass());
        }

    }

    @Test
    public void testConstructor()
    {
        // 测试 new Quantifier(num, unit)的行为
        // num有 3个等价类: 负数↓, 零↓, 正数↑
        // unit有4个等价类: null↓, 空↓, 长度>=6↓, 长度1~5↑
        // 采用两个一元化分进行测试

        // 1.num为负数↓
        testConstructorException(-1, "X", ArgumentOutOfRangeException.class);
        testConstructorException(-916, "*", ArgumentOutOfRangeException.class);

        // 2.num为零↓
        testConstructorException(0, "擦", ArgumentOutOfRangeException.class);

        // 3.num为正数↑
        Quantifier a301 = new Quantifier(99, "箱");
        assertEquals("99箱", a301.toString());
        assertEquals(99, a301.getNum());
        assertEquals("箱", a301.getUnit());

        Quantifier a302 = new Quantifier(1111, "公斤");
        assertEquals("1111公斤", a302.toString());
        assertEquals(1111, a302.getNum());
        assertEquals("公斤", a302.getUnit());

        // 4.unit为null↓
        testConstructorException(12, null, ArgumentNullException.class);

        // 5.unit为空↓
        testConstructorException(12, "", ArgumentOutOfRangeException.class);

        // 6.unit.length >= 6↓
        testConstructorException(12, "大米亩产千万", ArgumentOutOfRangeException.class);
        testConstructorException(12, "长长长长长长长长长长长长长", ArgumentOutOfRangeException.class);

        // 7.unit.length ∈ [1,5]↑ 
        Quantifier a701 = new Quantifier(1, "个");
        assertEquals("1个", a701.toString());
        assertEquals(1, a701.getNum());
        assertEquals("个", a701.getUnit());

        Quantifier a702 = new Quantifier(12, "五个字单位");
        assertEquals("12五个字单位", a702.toString());
        assertEquals(12, a702.getNum());
        assertEquals("五个字单位", a702.getUnit());
    }

    public void testGetNum()
    {
        // test in testConstructor
    }

    public void testGetUnit()
    {
        // test in testUnit
    }
}
